package site.eblog.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import site.eblog.dao.ArticleMapper;
import site.eblog.entity.Article;
import site.eblog.entity.User;
import site.eblog.service.ArticleService;
import site.eblog.service.UserService;

@Controller
public class ArticleController {

	@Autowired ArticleService articleService;
	@Autowired UserService userService;
	
	@RequestMapping("getArticlesByName")
	@ResponseBody
	public int getArticlesByName(HttpServletRequest request){
		String userName = request.getParameter("author");
		Subject currentUser = SecurityUtils.getSubject();
		if(userName == null){
			userName = currentUser.getPrincipal().toString();
		}
		int res = articleService.getArticlesByName(userName);
		return res;
	}
	
	@RequestMapping("getYCArticlesByName")
	@ResponseBody
	public int getYCArticlesByName(HttpServletRequest request){
		String userName = request.getParameter("author");
		int res = articleService.getYCArticlesByName(userName);
		return res;
	}
	
	@RequestMapping("addArticle")
	@ResponseBody
	public int addArticle(@RequestBody Article article){
		Subject currentUser = SecurityUtils.getSubject();
		String author = currentUser.getPrincipal().toString();
		User user = userService.findByName(author);
		article.setAuthor(author);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		article.setPostD(sdf.format(new Date()));// new Date()为获取当前系统时间
		article.setUserPic(user.getUserPic());
		int res = articleService.addArticle(article);
		return res;
	}
	
	@RequestMapping("getArticleByAuthor")
	@ResponseBody
	public Map<String,Object> getArticleByAuthor(HttpServletRequest request){
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String tag = request.getParameter("tag").equals("")?null:request.getParameter("tag");
		String title = request.getParameter("title").equals("")?null:request.getParameter("title");
		Map<String,Object> resMap = new HashMap<>();
		Article article = new Article();
		Subject currentUser = SecurityUtils.getSubject();
		String author = currentUser.getPrincipal().toString();
		article.setAuthor(author);
		article.setTag(tag);
		article.setTitle(title);
		List<Article> articles = new ArrayList<>();
		PageHelper.startPage(page, pageSize);
		articles = articleService.getArticleByAuthor(article);
		PageInfo pageInfo = new PageInfo(articles);
		resMap.put("pageInfo", pageInfo);
		return resMap;
	}
	
	@RequestMapping("getArticleById")
	@ResponseBody
	public Article getArticleById(HttpServletRequest request){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		Article article = new Article();
		String id = session.getAttribute("eidtArticleId").toString();
		article = articleService.getArticleById(id);
		article.setContent(StringEscapeUtils.unescapeHtml(article.getContent()));
		return article;
		
	}
	
	@RequestMapping("upDateArticle")
	@ResponseBody
	public int upDateArticle(@RequestBody Article article){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String id = session.getAttribute("eidtArticleId").toString();
		article.setId(Integer.valueOf(id));
		int res = articleService.upDateArticle(article);
		return res;
	}
	
	@RequestMapping("delArticle")
	@ResponseBody
	public int delArticle(HttpServletRequest request){
		int id = Integer.valueOf(request.getParameter("id"));
		int res =  articleService.delArticle(id);
		return res;
	}
	
	@RequestMapping("batchDelArticle")
	@ResponseBody
	public int batchDelArticle(HttpServletRequest request){
		String rqList = request.getParameter("list");
		String[] ArrayList = rqList.split(",");
		int res = articleService.batchDelArticle(ArrayList);
		return res;
	}
	
	@RequestMapping("articleSelect")
	@ResponseBody
	public List<Article> articleSelect(HttpServletRequest request){
		String tag = request.getParameter("tag");
		String title = request.getParameter("title");
		Map<String,String> map = new HashMap<>();
		map.put("tag", tag);
		map.put("title", title);
		List<Article> articles = articleService.articleSelect(map);
		return articles;
	}
	
	@RequestMapping("mgGetArticleByAuthor")
	@ResponseBody
	public Map<String,Object> mgGetArticleByAuthor(HttpServletRequest request){
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		String tag = request.getParameter("tag").equals("")?null:request.getParameter("tag");
		String title = request.getParameter("title").equals("")?null:request.getParameter("title");
		String author = request.getParameter("author").equals("")?null:request.getParameter("author");
		Map<String,Object> resMap = new HashMap<>();
		Article article = new Article();
		article.setAuthor(author);
		article.setTag(tag);
		article.setTitle(title);
		List<Article> articles = new ArrayList<>();
		PageHelper.startPage(page, pageSize);
		articles = articleService.getArticleByAuthor(article);
		PageInfo pageInfo = new PageInfo(articles);
		resMap.put("pageInfo", pageInfo);
		return resMap;
	}
	
	@RequestMapping("listGetArticle")
	@ResponseBody
	public Map<String,Object> listGetArticle(HttpServletRequest request){
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String tag = session.getAttribute("listTag").toString();
		Map<String,String> map = new HashMap<>();
		map.put("tag", tag);
		
		Map<String,Object> resMap = new HashMap<>();
		List<Article> articles = new ArrayList<>();
		PageHelper.startPage(page, pageSize);
		articles = articleService.articleSelect(map);
		PageInfo pageInfo = new PageInfo(articles);
		resMap.put("pageInfo", pageInfo);
		return resMap;
	}
	
	@RequestMapping("readTop")
	@ResponseBody
	public Map<String,Object> readTop(HttpServletRequest request){
		Map<String,Object> map = new HashMap<>();
		List<Article> articles = new ArrayList<>();
		String author = request.getParameter("author");
		PageHelper.startPage(1, 8);
		articles = articleService.readTop(author);
		PageInfo pageInfo = new PageInfo(articles);
		map.put("pageInfo", pageInfo);
		return map;
	}
	
	@RequestMapping("tuijianTop")
	@ResponseBody
	public Map<String,Object> tuijianTop(HttpServletRequest request){
		Map<String,Object> map = new HashMap<>();
		List<Article> articles = new ArrayList<>();
		String author = request.getParameter("author");
		PageHelper.startPage(1, 8);
		articles = articleService.tuijianTop(author);
		PageInfo pageInfo = new PageInfo(articles);
		map.put("pageInfo", pageInfo);
		return map;
	}
	
	@RequestMapping("contentGetArticle")
	@ResponseBody
	public Article contentGetArticle(){
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String contentId = session.getAttribute("contentId").toString();
		Article article = articleService.getArticleById(contentId);
		article.setContent(StringEscapeUtils.unescapeHtml(article.getContent()));
		String readN = article.getReadN();
		Map<String,Object> upDateMap = new HashMap<>();
		upDateMap.put("id", contentId);
		upDateMap.put("readN", Integer.parseInt(readN)+1);
		int upDateRes = articleService.upDateReadNById(upDateMap);
		article.setReadN(String.valueOf((Integer.parseInt(readN)+1)));
		return article;
	}
	
	@RequestMapping("searchArticleByTitle")
	@ResponseBody
	public Map<String,Object> searchArticleByTitle(HttpServletRequest request){
		int page = Integer.parseInt(request.getParameter("page"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String title=session.getAttribute("searchText").toString();
		Map<String,Object> resMap = new HashMap<>();
		List<Article> articles = new ArrayList<>();
		PageHelper.startPage(page, pageSize);
		articles = articleService.searchArticleByTitle(title);
		PageInfo pageInfo = new PageInfo(articles);
		resMap.put("pageInfo", pageInfo);
		return resMap;
	}
	
	@RequestMapping("tuijianCtrl")
	@ResponseBody
	public Map<String,Object> tuijianCtrl(){
		int res = 0;
		Map<String,Object> resMap = new HashMap<>();
		Map<String,Object> upDmap = new HashMap<>();
		Subject currentUser = SecurityUtils.getSubject();
		Session session = currentUser.getSession();
		String contentId = session.getAttribute("contentId").toString();
		String loginName = currentUser.getPrincipal().toString();
		Article article = articleService.getArticleById(contentId);
		String recommendN = article.getRecommendN();
		String recommended = article.getRecommended()==null?"":article.getRecommended();
		int tuijian = recommended.indexOf(loginName);
		if(tuijian == -1){
			upDmap.put("recommendN", Integer.parseInt(recommendN)+1);
			upDmap.put("recommended",recommended+","+loginName);
			upDmap.put("id",contentId);
			int upRes = articleService.upDateRecommendNById(upDmap);
			resMap.put("res",upRes);
			resMap.put("recommendN", Integer.parseInt(recommendN)+1);
		}else{
			resMap.put("res",res);
			resMap.put("resMsg","您已经推荐过了。");
		}
		return resMap;
		
	}
}
